import os
import cv2
from xml.etree.ElementTree import ElementTree,Element

for file in os.listdir('/home/chenzl/my/objection_detection_under_water/VOC2020/JPEGImages/'):
    img_file = os.path.join('/home/chenzl/my/objection_detection_under_water/VOC2020/JPEGImages/', file)
    img = cv2.imread(img_file)
    width_img = img.shape[1]
    height_img = img.shape[0]

    xml_file = file.split('.')[0]+'.xml'
    tree = ElementTree()
    tree.parse('/home/chenzl/my/objection_detection_under_water/VOC2020/Annotation/'+xml_file)  # xml文件
    root = tree.getroot()
    element_sz = Element('size')
    width = Element('width')
    width.text = str(width_img)
    height = Element('height')
    height.text = str(height_img)
    element_sz.append(width)
    element_sz.append(height)
    root.append(element_sz)
    tree.write('/home/chenzl/my/objection_detection_under_water/VOC2020/Annotation/'+xml_file, encoding='UTF-8',
               xml_declaration=True)





